package com.lwj.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lwj.hiring.common.util.AppUtil;
import com.lwj.hiring.company.dao.ApplicationDao;
import com.lwj.hiring.company.dao.PositionDao;
import com.lwj.hiring.company.entity.ApplicationEntity;
import com.lwj.hiring.company.entity.PositionEntity;

/**
 * ApplicationServlet
 * 
 * @author Administrator
 *
 */
public class ApplicationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ApplicationDao applicationDao = new ApplicationDao();
	private PositionDao positionDao = new PositionDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("delete".equals(command)) {
			delete(request, response);
		} else if ("apply".equals(command)) {
			apply(request, response);
		} else if ("verify".equals(command)) {
			verify(request, response);
		} else if ("listCompanyVerify".equals(command)) {
			listCompanyVerify(request, response);
		}
	}

	/**
	 * 根据审核状态查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void listCompanyVerify(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int companyId = AppUtil.getCompanyId(request, response);
		int status = Integer.parseInt(request.getParameter("status"));
		String positionName = request.getParameter("positionName");

		// 查询出来的被投递的职位信息
		List<ApplicationEntity> applicationList = applicationDao
				.listCompanyVerify(companyId, status, positionName);
		request.setAttribute("applicationList", applicationList);
		// 职位列表
		List<PositionEntity> positionList = positionDao.list(companyId);
		request.setAttribute("positionList", positionList);
		// 查询选择框显示
		request.setAttribute("status", status);

		request.getRequestDispatcher("view/company/main.jsp").forward(request,
				response);
	}

	/**
	 * 审核
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void verify(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int status = Integer.parseInt(request.getParameter("status"));
		int applicationId = Integer.parseInt(request
				.getParameter("applicationId"));

		int row = applicationDao.verify(status, applicationId);
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 投递简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void apply(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int resumeId = Integer.parseInt(request.getParameter("resumeId"));
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		int talentId = AppUtil.getTalentId(request, response);
		int row = applicationDao.add(talentId, resumeId, positionId);
		if (row > 0) {
			response.sendRedirect("PositionServlet?command=detail&id="
					+ positionId);
		}
	}

	/**
	 * 撤销这位申请
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int row = applicationDao.delete(id);
		response.sendRedirect("TalentServlet?command=main&row=" + row);
	}

}
